package com.njwb.day15.zqzb.jdbc02;

import java.sql.*;
public class DeleteStudentTest {
    public static void main(String[] args) {
        int sid = 7;
        //构建连接对象
        Connection connection= null ;
        //命令器
        PreparedStatement pstmt= null ;
        try {
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest","root","root");
            //3、创建命令器
            pstmt = connection.prepareStatement("delete from tb_student where sid =?");
            pstmt.setInt(1,sid);
            //5.执行sql语句
            pstmt.executeUpdate();
            System.out.println("数据删除成功！！！");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6、关闭资源 （倒着关闭）
            if(pstmt!=null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
